"""
    Author:
    Date: 2019-10-26

    动态表情管理类
"""

from entities.consts import *
from utils import db


def get_animoji_list():
    """ 获取动态表情列表 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_animoji
        ORDER BY    enabled DESC,
                    orderno,
                    id;
    '''
    data = db.query_data_from_db(sql)
    return data if data else []


def enable_animoji(animoji_id):
    """ 启用动态表情 """
    sql = f'''
        UPDATE      cmf_config_animoji
        SET         enabled=1
        WHERE       animoji_id={animoji_id} AND
                    enabled=0;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def disable_animoji(animoji_id):
    """ 禁用动态表情 """
    sql = f'''
        UPDATE      cmf_config_animoji
        SET         enabled=0
        WHERE       animoji_id={animoji_id} AND
                    enabled=1;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def new_animoji(id, name, resource, orderno):
    """ 添加新的动态表情 """
    sql = '''
        INSERT INTO cmf_config_animoji
        SET         animoji_id=%(id)s,
                    animoji_name=%(name)s,
                    animoji_url=%(resource)s,
                    orderno=%(orderno)s,
                    enabled=0
    '''
    params = {
        'id': str(id),
        'name': name,
        'resource': resource,
        'orderno': str(orderno),
    }
    affected = db.execute_sql(sql, params)
    return affected == 1


def edit_animoji(animoji_id, name, resource, orderno):
    """ 编辑动态表情 """
    sql = f'''
        UPDATE      cmf_config_animoji
        SET         animoji_name=%(name)s,
                    animoji_url=%(resource)s,
                    orderno=%(orderno)s
        WHERE       animoji_id=%(animoji_id)s;
    '''
    params = {
        'animoji_id': str(animoji_id),
        'name': name,
        'resource': resource,
        'orderno': str(orderno),
    }
    affected_rows = db.execute_sql(sql, params)
    return affected_rows > 0


def get_max_animoji_id():
    """ 获取最大的动态表情id """
    sql = '''
        SELECT    MAX(animoji_id) AS max_animoji_id
        FROM      cmf_config_animoji;
    '''
    data = db.query_data_from_db(sql)
    return data[0]['max_animoji_id'] if data else 0


def get_animoji(animoji_id):
    """ 根据id获取礼物 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_animoji
        WHERE       animoji_id={animoji_id};
    '''
    data = db.query_data_from_db(sql)
    return data[0] if data else None


if __name__ == '__main__':
    print('ok')
